package com.zxu.test.studyapp;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.Observable;
import androidx.lifecycle.ViewModelProviders;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.zxu.test.EventBusUtil.AppEventMsg;
import com.zxu.test.EventBusUtil.BroadcastEvent;
import com.zxu.test.application.MyApplication;
import com.zxu.test.base.BaseFragment;
import com.zxu.test.studyapp.databinding.ActivityMainBinding;
import com.zxu.test.util.Event;
import com.zxu.test.view.ToastCustom;
import org.greenrobot.eventbus.EventBus;
import java.io.File;
import java.io.IOException;

public class MainFragment extends BaseFragment {
    private ActivityMainBinding mBinding;
    private MainViewModel mViewModel;
    public static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "autograph.png";
    //本地广播 快 外部app接收不到
    private LocalBroadcastManager lbm;
    public MainFragment() { }

    public static MainFragment getInstance() {
        return new MainFragment();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = ActivityMainBinding.inflate(inflater, container, false);
        mViewModel = ViewModelProviders.of(getActivity()).get(MainViewModel.class);
        mBinding.setViewModel(mViewModel);
        mBinding.signatureview.setBackColor(getResources().getColor(R.color.white));
        mBinding.signatureview.setPenColor(getResources().getColor(R.color.black));
        lbm = LocalBroadcastManager.getInstance(getContext());
        return  mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupListener();
    }

    private void setupListener() {
        mViewModel.getBroadcastCommand().observe(getViewLifecycleOwner(), this::goToSendBrodcast);
        mViewModel.btnEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                ToastCustom.getInstance(MyApplication.getInstance()).show(mViewModel.btnEvent.get(),1000);
                EventBus.getDefault().post(new AppEventMsg(AppEventMsg.SPLASH_FRAGMENT_MSG));
            }
        });
        mViewModel.clearEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                mBinding.signatureview.clear();
            }
        });
        mViewModel.cancelEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                ToastCustom.getInstance(MyApplication.getInstance()).show("取消",1000);
            }
        });
        mViewModel.saveEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                if (mBinding.signatureview.getTouched()) {
                    try {
                        mBinding.signatureview.save(path, false, 10);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastCustom.getInstance(MyApplication.getInstance()).show("null",1000);
                }
            }
        });
    }

    private void goToSendBrodcast(Event<Object> taskIdEvent) {
        String str = taskIdEvent.getContentIfNotHandled().toString();
        ToastCustom.getInstance(MyApplication.getInstance()).show(str,1000);
        lbm.sendBroadcast(new Intent(BroadcastEvent.SEND_LOCALBROADCAST_MSG));
    }

    @Override
    public void onResume() {
        if(getActivity().getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onResume();
    }
}
